﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test055 : MonoBehaviour
{
    public GameObject objMove;
    // Start is called before the first frame update
    void Start()
    {
        Application.targetFrameRate = 30;
        objMove.SetActive(false);
    }

    private Vector2 pos;
    private Vector2 oldPos;
    private Vector2 oldDir;
    private Vector2 distance;
    private float angle;

    private string txt;
    // Update is called once per frame
    void Update()
    {
        // Debug.Log(Input.touchCount);
        // for (int i = 0; i < Input.touchCount; i++)
        // {
        //     Debug.Log(Input.touches[i].fingerId);
        //     Debug.Log(Input.GetTouch(i).position);
        // }
        
        // for (int i = 0; i < LeanTouch.Fingers.Count; i++)
        // {
        //     Debug.Log(LeanTouch.Fingers[i].Index);
        // }
        
        // LeanTouch.Fingers.Count

        if (Input.touchCount==3)
        {
            objMove.SetActive(true);
            //Debug.Log("");
            var pos = (Input.touches[0].position + Input.touches[1].position + Input.touches[2].position) / 3f;
            //Debug.Log(pos);
            var dir = (Input.GetTouch(1).position - Input.GetTouch(0).position).normalized;
            angle = Vector2.Angle(dir, oldDir);
            
            float dotValue = Vector3.Dot(dir, oldDir);
            var crossValue =  Vector3.Cross(dir, oldDir);
            
            distance = (pos-oldPos);
            oldDir = dir;
            oldPos = pos;
            objMove.transform.position = pos;
           
            if (crossValue.z<0)
            {
                objMove.transform.Rotate(Vector3.forward,angle);
                txt = "逆时针";
            }
            else if(crossValue.z>0)
            {
                objMove.transform.Rotate(Vector3.forward,-angle);
                txt = "顺时针";
            }
        }
        
        
        
    }

    private void OnGUI()
    {
        GUI.Label(new Rect(50,50,200,30),"旋转方向"+txt);
        GUI.Label(new Rect(50,100,200,30),"旋转角度"+angle.ToString());
        GUI.Label(new Rect(50,150,200,30),"移动距离"+distance.ToString());
        GUI.Label(new Rect(50,200,200,30),"Touch"+Input.touchCount);
    }
}
